package com.prosay.jdbc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * 
* @ClassName: Handler 
* @Description:用于处理用户的单个请求的子线程处理类 
* @author 猿说教育-Jame
* @date 2017年6月3日 下午9:37:31 
*
 */
public class Handler implements Runnable{

	private Socket socket;
	/**
	 * 
	 * @param socket
	 */
	public Handler(Socket socket){
		this.socket = socket;
	}
	/**
	 * 
	* @Title: run 
	* @Description: 执行子线程的处理
	* @return void    返回类型 
	* @throws
	 */
	public  void run(){
		try{
			InputStream in = socket.getInputStream();
			//字节流转字符流
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			StringBuilder str = new StringBuilder();
			String tmp = "";
			while((tmp=reader.readLine())!=null&&tmp.length()>0){
				str.append(tmp);
				str.append("\r\n");
			}
			System.out.println(str.toString());
			//d:\\web 作为系统的根目录
			String webRoot = "d:\\web";
			String[] msgs = str.toString().split(" ");
			OutputStream out  = socket.getOutputStream();
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(out));
			writer.println("HTTP/1.1 200 OK");
			writer.println("Content-Type: text/html;charset=utf-8");
			writer.println();
			writer.flush();
			if(msgs.length>2){
				FileInputStream fileIn = new FileInputStream(new File(webRoot+msgs[1]));
				byte[] buf = new byte[1024];
				int length = 0;		
				while((length=fileIn.read(buf))!=-1){
					out.write(buf,0,length);
				}
				fileIn.close();
				out.close();
			}
		}catch(Exception ex){
			
		}
	}
}
